<?php

class InvoiceProductForm extends BaseForm {

    public function __construct($suffix = null) {
        $this->index = ($suffix) ? $suffix : '';
        $this->required = ($suffix == '_0')?false:true;
        parent::__construct();
    }
    public function setup() {
      $products  = $this->getProductsFormatted();
      $units     = $this->getUnitsFormatted();

        $this->setWidgets(array(
            'product_id'. $this->index  => new sfWidgetFormChoice(array('choices' => $products)),
            'unit_id'  . $this->index   => new sfWidgetFormChoice(array('choices' => $units)),
            'price'    . $this->index   => new sfWidgetFormInputText(),
            'qty'     . $this->index    => new sfWidgetFormInputText(),
        ));

        
        $this->setValidators(array(
            'product_id'. $this->index   => new sfValidatorChoice(array('choices' => array_keys($products), 'required' => $this->required), array('invalid'=>'Invalid Product.','required'=>'Please enter required field.')),
            'unit_id'   . $this->index   => new sfValidatorChoice(array('choices' => array_keys($units), 'required' => $this->required), array('invalid'=>'Invalid Unit.','required'=>'Please enter required field.')),
            'price'     . $this->index   => new sfValidatorNumber(array('min'=> 0.01, 'required' => $this->required , 'max'=> 99999999.99), array('min'=>'Invalid Price.', 'invalid'=>'Invalid Price.','max'=>'Invalid Price.','required'=>'Please enter required field.')),
            'qty'       . $this->index   => new sfValidatorNumber(array('min'=> 0.01, 'required' => $this->required , 'max'=> 99999999.99), array('min'=>'Invalid Quantity.', 'invalid'=>'Invalid Quantity.','max'=>'Invalid Quantity.','required'=>'Please enter required field.')),
        ));

        $this->widgetSchema->setNameFormat('invoiceProduct[%s]');
    }

        
    private function getProductsFormatted() {
        $bank_accounts = Doctrine_Query::create()->from('Product')->where('is_deleted=?', 0)->orderBy('name')->fetchArray();
        $result = array(''=>'');
        
        foreach ($bank_accounts as $bank_account) {
            $result[$bank_account['id']] = $bank_account['name'];
        }
        return $result;
    }
        
    private function getUnitsFormatted() {
        $bank_accounts = Doctrine::getTable("Unit")->findAll();
        $result = array(''=>'');
        
        foreach ($bank_accounts as $bank_account) {
            $result[$bank_account['id']] = $bank_account['name'];
        }
        return $result;
    }
    
    
}

?>
